package com.indexingsystem.system.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import com.indexingsystem.system.service.SchedulerManageService;
import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.TasksIdVo;
import com.indexingsystem.boss.Vo.TasksQueryVo;
import com.indexingsystem.boss.Vo.TasksVo;
import com.indexingsystem.boss.admin.mapper.TasksMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.Tasks;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.system.service.ITasksService;

@Service
@Transactional(readOnly = true) 
public class TasksServiceImpl implements ITasksService {
    private static Logger LogCvt = LoggerFactory.getLogger(TasksServiceImpl.class);
   
	@Resource
	private TasksMapper tasksMapper;
	
//	@Resource
//	private SchedulerManageService schedulerManageService;
	

//    private Scheduler scheduler = (Scheduler) SpringUtils.getBeanById("scheduler"); 

	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
	    Integer result = 0;
	    Tasks tasks =  this.selectByPrimaryKey(id);
	    result = tasksMapper.deleteByPrimaryKey(id);	    
		   //结束任务  
//	    schedulerManageService.deleteJob(tasks);
//	    SchedulerManage.endTask(tasks.getTaskName()); 
        return result;
	}

	@Override
	public int deleteByIdBatch(List<String> ids)throws Exception {
		int count = 0;
		for (String id : ids) {
		    Tasks tasks =  this.selectByPrimaryKey(id);
			count += tasksMapper.deleteByPrimaryKey(id);
			//结束任务  
//			 schedulerManageService.deleteJob(tasks);
//			SchedulerManage.endTask(tasks.getTaskName()); 
		}
		return count;
	}

	@Override
	public int insert(Tasks tasks) throws Exception{
	    int result = 0;
	    result= tasksMapper.insert(tasks);
	    // 启用则启动任务  
//        if (tasks.getTaskStatus() == 1) {  
//        	schedulerManageService.addJobCron(tasks);
//        	SchedulerManage.startTask(tasks, tasks.getTaskName());  
//        }  
        return result;        
	}

	@Override
	public int insertSelective(Tasks tasks)throws Exception {
	    int result = 0;
        result= tasksMapper.insertSelective(tasks);
        // 启用则启动任务  
//        if (tasks.getTaskStatus() == 1) {
//        	schedulerManageService.addJobCron(tasks);
//        	SchedulerManage.startTask(tasks, tasks.getTaskName());  
//        }  
        return result; 
	}

	@Override
	public Tasks selectByPrimaryKey(String id) {
		return tasksMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public Tasks selectTaskName(String name) {
		return tasksMapper.selectTaskName(name);
	}

	@Override
	public int updateByPrimaryKeySelective(Tasks record) throws Exception{
	    int result = 0;
	    //结束定时调度  
//	    SchedulerManage.endTask(record.getTaskName());  
//	    schedulerManageService.updateJobCron(record);
        
        result =  tasksMapper.updateByPrimaryKeySelective(record);
	    
        // 启用则启动任务  
//        if (record.getTaskStatus() == 1) {  
//        	SchedulerManage.startTask(record, record.getTaskName());  
//        }  	    
//	    
		return result;
	}

	@Override
	public int updateByPrimaryKey(Tasks record)throws Exception {
	    int result = 0;
        //结束定时调度  
	   // SchedulerManage.endTask(record.getTaskName());  
        
        result =  tasksMapper.updateByPrimaryKey(record);
//        schedulerManageService.updateJobCron(record);
        
        // 启用则启动任务  
//        if (record.getTaskStatus() == 1) {  
//        	SchedulerManage.startTask(record, record.getTaskName());  
//        }               
        return result;
	}

	@Override
	public PagedResult<TasksVo> selectTasksList(Integer pageNo, Integer pageSize) {
		pageNo = pageNo == null ? 1 : pageNo;
		pageSize = pageSize == null ? 10 : pageSize;
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage(pageNo, pageSize);
		return BeanUtils.toPagedResult(tasksMapper.selectTasksList(null)); //
	}

	@Override
	public PagedResult<TasksVo> selectTasksList(TasksQueryVo tasksVo) {
		Integer pageNo = tasksVo.getPageNo() == null ? 0 : tasksVo.getPageNo();
		Integer pageSize = tasksVo.getPageSize() == null ? 10 : tasksVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage(pageNo, pageSize);
		return BeanUtils.toPagedResult(tasksMapper.selectTasksList(tasksVo));
	}

	@Override
	public Integer getTasksListCount(TasksQueryVo tasksVo) {
		return tasksMapper.getTasksListCount(tasksVo);
	}

	@Override
	public int updateStatusByPrimaryKey(TasksIdVo taskIdVo) throws Exception{
		int count = 0;
		Integer status = taskIdVo.getStatus();
		List<String> ids = taskIdVo.getIds();
		for (String id : ids) {
			count += tasksMapper.updateStatusByPrimaryKey(id, status);
		}
		return count;
	}
  
	/**
	 * 通过定时属性查询定时集合.
	 */
	@Override
    public List<Tasks> findList() {  
        return tasksMapper.findList();  
    }      
}